﻿// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Mrc.Config
{
    public static class  StreamHelpers
    {
        public static readonly string ArbitraryFilePath = "Unit tests do not touch file system";

        public static Stream StringToStream(string str)
        {
            var memStream = new MemoryStream();
            var textWriter = new StreamWriter(memStream);
            textWriter.Write(str);
            textWriter.Flush();
            memStream.Seek(0, SeekOrigin.Begin);

            return memStream;
        }

        public static string StreamToString(Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream);

            return reader.ReadToEnd();
        }

        private static readonly Encoding DefaultEncoding = Encoding.UTF8;
        public static string StreamToUTF8String(Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream,DefaultEncoding);

            return reader.ReadToEnd();
        }

        public static string StreamString(Stream stream, Encoding encoding)
        {
            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream, encoding);
            return reader.ReadToEnd();
        }
    }

}
